//
//  PersonalFirstInfoViewController.m
//  Btpin
//
//  Created by Vic on 2019/3/12.
//  Copyright © 2019 Vic. All rights reserved.
//

#import "PersonalFirstInfoViewController.h"
#import "PersonalEducationInfoViewController.h"

#import "AppDelegate.h"
#import "IDSelectViewController.h"
#import "PersonalWorkExperienceViewController.h"
#import "CompanyFirstInfoViewController.h"
#import "CompanySecondInfoViewController.h"
#import "CompanyInfoViewController.h"
#import "RegisterIntentionViewController.h"

@interface PersonalFirstInfoViewController ()<TimeSelectPickerViewDelegate, PSSingleRowViewDelegate, RegisterIntentionViewControllerDelegate>
{
    UIButton *businessmanBtn;
    UIButton *studentBtn;
    
    NSString *is_graduate;
    NSString *sex;
    NSString *nature;
    NSString *current;
//    NSString *trade;
    NSString *experience;
    
    PMineResumeIntentionInfoModel *RegisterIntentionModel;
}
@property (nonatomic, strong) PSCell *nameCell;
@property (nonatomic, strong) PSCell *birthdayCell;
@property (nonatomic, strong) PSCell *genderCell;
@property (nonatomic, strong) PSCell *workCell;
@property (nonatomic, strong) PSCell *statusCell;
//    PSCell *categoryCell;
@property (nonatomic, strong) PSCell *experienceCell;
@property (nonatomic, strong) TimeSelectPickerView *birthdayPickerView;
@property (nonatomic, strong) PSSingleRowView *genderPickerView;
@property (nonatomic, strong) PSSingleRowView *workPickerView;
@property (nonatomic, strong) PSSingleRowView *statusPickerView;
//@property (nonatomic, strong) PSSingleRowView *categoryPickerView;
@property (nonatomic, strong) PSSingleRowView *experiencePickerView;
@end

@implementation PersonalFirstInfoViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
//    [self.navigationController setNavigationBarHidden:NO animated:YES];
    self.navigationController.navigationBarHidden = NO;
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [UIToolsControl resignFirstResponderForTextField:self.nameCell.textField];
    if (self.genderPickerView) {
        self.genderPickerView.hidden = YES;
    }
    if (self.workPickerView) {
        self.workPickerView.hidden = YES;
    }
    if (self.statusPickerView) {
        self.statusPickerView.hidden = YES;
    }
//    if (self.categoryPickerView) {
//        self.categoryPickerView.hidden = YES;
//    }
    if (self.experiencePickerView) {
        self.experiencePickerView.hidden = YES;
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    is_graduate = @"0";
    sex = @"";
    nature = @"";
    current = @"";
//    trade = @"";
    experience = @"";
    RegisterIntentionModel = [[PMineResumeIntentionInfoModel alloc] init];
    
    [self initUI];
}
- (void)initUI {
    self.navigationItem.title = @"欢迎您来到比特聘";
    
    if (self.isRegisterPush) {
//        [self.navigationController popViewControllerAnimated:YES];
    } else {
        UIButton *switchBtn = [UIToolsControl initButtonWithFrame:CGRectMake(0, 0, 60*Scale, 44) imageName:nil target:self action:@selector(switchBtnTap) title:@"  我要招人" titleColor:THEMECOLOR_OF_ORANGE];
        switchBtn.titleLabel.font = Font(Font_15);
        UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:switchBtn];
        self.navigationItem.leftBarButtonItem = leftItem;
    }
    
    UIButton *nextBtn = [UIToolsControl initButtonWithFrame:CGRectMake(WIDTH-60*Scale, HeightOfStatusBar+3*Scale, 60*Scale, 36) imageName:nil target:self action:@selector(nextBtnTap) title:@"下一步" titleColor:THEMECOLOR_OF_ORANGE];
    nextBtn.titleLabel.font = Font(Font_15);
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:nextBtn];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    UILabel *topLbl = [UIToolsControl initLabelWithFrame:CGRectMake(0, 20*Scale, WIDTH, 30*Scale) text:@"真实完整的个人简历，不但找工作快，还可以赚钱" textColor:THEMECOLOR_OF_TEXT_9a9a9a font:Font_15];
    topLbl.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:topLbl];
    
    businessmanBtn = [UIToolsControl initButtonWithFrame:CGRectMake(WIDTH*0.5-100*Scale, EndY(topLbl)+20*Scale, 100*Scale, 40*Scale) imageName:nil target:self action:@selector(businessmanBtnTap) title:@"职场人士" titleColor:COLOR_OF_WHITE];
    businessmanBtn.titleLabel.font = Font(Font_17);
    businessmanBtn.backgroundColor = THEMECOLOR_OF_ORANGE;
    businessmanBtn.layer.borderWidth = 0.5;
    businessmanBtn.layer.borderColor = COLOR_OF_WHITE.CGColor;
    [self.view addSubview:businessmanBtn];
    businessmanBtn.selected = YES;
    
    studentBtn = [UIToolsControl initButtonWithFrame:CGRectMake(EndX(businessmanBtn), StartY(businessmanBtn), ViewWidth(businessmanBtn), ViewHeight(businessmanBtn)) imageName:nil target:self action:@selector(studentBtnTap) title:@"应届学生" titleColor:THEMECOLOR_OF_TEXT_666666];
    studentBtn.titleLabel.font = Font(Font_17);
    studentBtn.backgroundColor = COLOR_OF_WHITE;
    studentBtn.layer.borderWidth = 0.5;
    studentBtn.layer.borderColor = THEMECOLOR_OF_TEXT_9a9a9a.CGColor;
    [self.view addSubview:studentBtn];
    studentBtn.selected = NO;
    
    __weak __typeof(self)weakSelf = self;
    self.birthdayPickerView = [[TimeSelectPickerView alloc] initWithTarget:self time:nil timeSelectPickerType:TimeSelectPickerTypeOfBirthday dateFormat:nil];
    self.birthdayPickerView.hidden = YES;
    self.genderPickerView = [[PSSingleRowView alloc] initWithData:[DataToolsControl sharedManager].allCategoryInfoModel.sexArray target:self];
    self.genderPickerView.hidden = YES;
    self.workPickerView = [[PSSingleRowView alloc] initWithData:[DataToolsControl sharedManager].allCategoryInfoModel.jobsNatureArray target:self];
    self.workPickerView.hidden = YES;
    self.statusPickerView = [[PSSingleRowView alloc] initWithData:[DataToolsControl sharedManager].allCategoryInfoModel.currentArray target:self];
    self.statusPickerView.hidden = YES;
//    self.categoryPickerView = [[PSSingleRowView alloc] initWithData:[DataToolsControl sharedManager].allCategoryInfoModel.tradeArray target:self];
//    self.categoryPickerView.hidden = YES;
    self.experiencePickerView = [[PSSingleRowView alloc] initWithData:[DataToolsControl sharedManager].allCategoryInfoModel.experienceArray target:self];
    self.experiencePickerView.hidden = YES;
    
    self.nameCell = [[PSCell alloc] initWithFrame:CGRectMake(30*Scale, EndY(businessmanBtn)+15*Scale, WIDTH-60*Scale, PSCellHeight) title:@"姓名" text:(UD_GetOForK(isCompanyUserInfoName)?UD_GetOForK(isCompanyUserInfoName):nil) placeholder:@"请输入姓名" cellType:PSCellTypeOfWriteonly];
    [self.view addSubview:self.nameCell];
    
    self.birthdayCell = [[PSCell alloc] initWithFrame:CGRectMake(30*Scale, EndY(self.nameCell), WIDTH-60*Scale, PSCellHeight) title:@"生日" text:nil placeholder:@"年/月/日" cellType:PSCellTypeOfIndicator];
    self.birthdayCell.tap = ^{
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        [UIToolsControl resignFirstResponderForTextField:strongSelf.nameCell.textField];
        strongSelf.birthdayPickerView.hidden = NO;
    };
    [self.view addSubview:self.birthdayCell];
    
    self.genderCell = [[PSCell alloc] initWithFrame:CGRectMake(30*Scale, EndY(self.birthdayCell), WIDTH-60*Scale, PSCellHeight) title:@"性别" text:nil placeholder:@"男/女" cellType:PSCellTypeOfIndicator];
    self.genderCell.tap = ^{
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        [UIToolsControl resignFirstResponderForTextField:strongSelf.nameCell.textField];
        strongSelf.genderPickerView.hidden = NO;
    };
    [self.view addSubview:self.genderCell];
    
    self.workCell = [[PSCell alloc] initWithFrame:CGRectMake(30*Scale, EndY(self.genderCell), WIDTH-60*Scale, PSCellHeight) title:@"求职意向" text:nil placeholder:@"请选择" cellType:PSCellTypeOfIndicator];
    self.workCell.tap = ^{
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        [UIToolsControl resignFirstResponderForTextField:strongSelf.nameCell.textField];
//        strongSelf.workPickerView.hidden = NO;
        [strongSelf pushIntentionVC];
    };
    [self.view addSubview:self.workCell];
    
    self.experienceCell = [[PSCell alloc] initWithFrame:CGRectMake(30*Scale, EndY(self.workCell), WIDTH-60*Scale, PSCellHeight) title:@"工作经验" text:nil placeholder:@"请选择" cellType:PSCellTypeOfIndicator];
    self.experienceCell.tap = ^{
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        [UIToolsControl resignFirstResponderForTextField:strongSelf.nameCell.textField];
        strongSelf.experiencePickerView.hidden = NO;
    };
    [self.view addSubview:self.experienceCell];
    
    self.statusCell = [[PSCell alloc] initWithFrame:CGRectMake(30*Scale, EndY(self.experienceCell), WIDTH-60*Scale, PSCellHeight) title:@"求职状态" text:nil placeholder:@"离职/在职" cellType:PSCellTypeOfIndicator];
    self.statusCell.tap = ^{
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        [UIToolsControl resignFirstResponderForTextField:strongSelf.nameCell.textField];
        strongSelf.statusPickerView.hidden = NO;
    };
    [self.view addSubview:self.statusCell];
    
//    categoryCell = [[PSCell alloc] initWithFrame:CGRectMake(30*Scale, EndY(workCell), WIDTH-60*Scale, PSCellHeight) title:@"期望行业" text:nil placeholder:@"请选择" cellType:PSCellTypeOfIndicator];
//    categoryCell.tap = ^{
//        [UIToolsControl resignFirstResponderForTextField:self->nameCell.textField];
//        __strong __typeof(weakSelf)strongSelf = weakSelf;
//        strongSelf.categoryPickerView.hidden = NO;
//    };
//    [self.view addSubview:categoryCell];
//    categoryCell.hidden = YES;
}
- (void)nextBtnTap {
    [UIToolsControl resignFirstResponderForTextField:self.nameCell.textField];
    if (self.nameCell.textField.text.length>0) {
        if (self.birthdayCell.textField.text.length>0) {
            if (self.genderCell.textField.text.length>0) {
                if (self.workCell.textField.text.length>0) {
                    if (businessmanBtn.isSelected) {
                        if (self.statusCell.textField.text.length>0) {
                            if (self.experienceCell.textField.text.length>0) {
                                [self upDate];
                            } else {
                                SVPHUDError(PersonalExperienceAlertText);
                            }
                        } else {
                            SVPHUDError(StatusAlertText);
                        }
                    } else {
//                        if (categoryCell.textField.text.length>0) {
//                            [self upDate];
//                        } else {
//                            SVPHUDError(CategoryAlertText);
//                        }
                        [self upDate];
                    }
                } else {
                    SVPHUDError(WorkAlertText);
                }
            } else {
                SVPHUDError(GenderAlertText);
            }
        } else {
            SVPHUDError(BirthdayAlertText);
        }
    } else {
        SVPHUDError(NameAlertText);
        [self.nameCell.textField becomeFirstResponder];
    }
}
- (void)upDate {
    SVPHUDShow;
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
    dic = [UIToolsControl getPublicParameter];
    [dic setObject:is_graduate forKey:@"is_graduate"];
    [dic setObject:self.nameCell.textField.text forKey:@"name"];
    [dic setObject:self.birthdayCell.textField.text forKey:@"birthday"];
    [dic setObject:sex forKey:@"sex"];
    [dic setObject:current forKey:@"current"];
    
    [dic setObject:RegisterIntentionModel.nature forKey:@"nature"];
    [dic setObject:RegisterIntentionModel.wage forKey:@"wage"];
    [dic setObject:RegisterIntentionModel.trade forKey:@"trade"];
    [dic setObject:RegisterIntentionModel.jobs_id forKey:@"job_cats"];
    [dic setObject:RegisterIntentionModel.district forKey:@"district"];
    [dic setObject:experience forKey:@"experience"];
    
    [BaseNetManager requestWithMethod:POST path:Personal_CreateResumeV2_URL params:dic successBlock:^(NSDictionary *responseObject) {
        if ([responseObject[@"code"] integerValue] == 2000) {
            SVPHUDSuccess(responseObject[@"msg"]);
            UD_SetOForK(responseObject[@"data"][@"resume_id"], isResumeID);
            
            PersonalEducationInfoViewController *PEIVC = [[PersonalEducationInfoViewController alloc] init];
            [self.navigationController pushViewController:PEIVC animated:YES];
        } else {
            NSString *str = [NSString stringWithFormat:@"%@", responseObject[@"msg"]];
            SVPHUDError(str);
        }
    } failurBlock:^(NSError *error) {
        SVPHUDError(NetErrorAlertText);
    }];
}
/*
- (void)upDate {
    SVPHUDShow;
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
    dic = [UIToolsControl getPublicParameter];
    [dic setObject:is_graduate forKey:@"is_graduate"];
    [dic setObject:nameCell.textField.text forKey:@"name"];
    [dic setObject:birthdayCell.textField.text forKey:@"birthday"];
    [dic setObject:sex forKey:@"sex"];
    [dic setObject:nature forKey:@"nature"];
    [dic setObject:trade forKey:@"trade"];
    [dic setObject:current forKey:@"current"];
    
    [BaseNetManager requestWithMethod:POST path:Personal_CreateResume_URL params:dic successBlock:^(NSDictionary *responseObject) {
        if ([responseObject[@"code"] integerValue] == 2000) {
            SVPHUDSuccess(responseObject[@"msg"]);
            UD_SetOForK(responseObject[@"data"][@"resume_id"], isResumeID);
            
            PersonalEducationInfoViewController *PEIVC = [[PersonalEducationInfoViewController alloc] init];
            [self.navigationController pushViewController:PEIVC animated:YES];
        } else {
            NSString *str = [NSString stringWithFormat:@"%@", responseObject[@"msg"]];
            SVPHUDError(str);
        }
    } failurBlock:^(NSError *error) {
        SVPHUDError(NetErrorAlertText);
    }];
}
*/
- (void)psSingleRowViewCancel:(PSSingleRowView *)psSingleRowView {
    psSingleRowView.hidden = YES;
}
- (void)psSingleRowViewOk:(PublicCategoryElementModel *)model PSSingleRowView:(PSSingleRowView *)psSingleRowView {
    psSingleRowView.hidden = YES;
    if (psSingleRowView == self.genderPickerView) {
        sex = model.id;
        self.genderCell.textField.text = model.name;
    } else if (psSingleRowView == self.workPickerView) {
        nature = model.id;
        self.workCell.textField.text = model.name;
    } else if (psSingleRowView == self.statusPickerView) {
        current = model.id;
        self.statusCell.textField.text = model.name;
    }
//    else if (psSingleRowView == self.categoryPickerView) {
//        trade = model.id;
//        categoryCell.textField.text = model.name;
//    }
    else if (psSingleRowView == self.experiencePickerView) {
        experience = model.id;
        self.experienceCell.textField.text = model.name;
    }
}
- (void)timeSelectPickerViewCancel:(TimeSelectPickerView *)timeSelectPickerView {
    timeSelectPickerView.hidden = YES;
}
- (void)timeSelectPickerViewOK:(NSString *)selectText timeSelectPickerView:(TimeSelectPickerView *)timeSelectPickerView {
    timeSelectPickerView.hidden = YES;
    self.birthdayCell.textField.text = selectText;
}

- (void)businessmanBtnTap {
    is_graduate = @"0";
    [DataToolsControl sharedManager].idType = IDTypeOfWorker;
    self.statusCell.hidden = NO;
//    categoryCell.hidden = YES;
    self.experienceCell.hidden = NO;
    businessmanBtn.selected = YES;
    businessmanBtn.backgroundColor = THEMECOLOR_OF_ORANGE;
    [businessmanBtn setTitleColor:COLOR_OF_WHITE forState:UIControlStateNormal];
    businessmanBtn.layer.borderColor = COLOR_OF_WHITE.CGColor;
    
    studentBtn.selected = NO;
    studentBtn.backgroundColor = COLOR_OF_WHITE;
    [studentBtn setTitleColor:THEMECOLOR_OF_TEXT_666666 forState:UIControlStateNormal];
    studentBtn.layer.borderColor = THEMECOLOR_OF_TEXT_9a9a9a.CGColor;
}
- (void)studentBtnTap {
    is_graduate = @"1";
    [DataToolsControl sharedManager].idType = IDTypeOfFreshGraduate;
    self.statusCell.hidden = YES;
//    categoryCell.hidden = NO;
    self.experienceCell.hidden = YES;
    studentBtn.selected = YES;
    studentBtn.backgroundColor = THEMECOLOR_OF_ORANGE;
    [studentBtn setTitleColor:COLOR_OF_WHITE forState:UIControlStateNormal];
    studentBtn.layer.borderColor = COLOR_OF_WHITE.CGColor;
    
    businessmanBtn.selected = NO;
    businessmanBtn.backgroundColor = COLOR_OF_WHITE;
    [businessmanBtn setTitleColor:THEMECOLOR_OF_TEXT_666666 forState:UIControlStateNormal];
    businessmanBtn.layer.borderColor = THEMECOLOR_OF_TEXT_9a9a9a.CGColor;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [UIToolsControl resignFirstResponderForTextField:self.nameCell.textField];
}
- (void)switchBtnTap {
    NSLog(@"切换角色");
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
    dic = [UIToolsControl getPublicParameter];
    [dic setObject:@"1" forKey:@"user_role"];
    [BaseNetManager requestWithMethod:POST path:Change_Role_URL params:dic successBlock:^(NSDictionary *responseObject) {
        if ([responseObject[@"code"] integerValue] == 2000) {
            UD_SetOForK(@"1", isUserRole);
            
            NSDictionary *data = responseObject[@"data"];
            if (data[@"user_guide"] && [data[@"user_guide"] length] && ([data[@"user_guide"] isEqualToString:@"choose_role"])) { // 用户还没选角色 （选择用户角色）
                IDSelectViewController *IDSVC = [[IDSelectViewController alloc] init];
                IDSVC.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:IDSVC animated:NO];
            } else {
                if ([data[@"user_guide"] isEqualToString:@"company_add_job"]) { // 添加工作
                    [DataToolsControl getMyBaseInfoForCompany:^(BOOL requestFlag) {
                        [SharedApp showViewController:TabBarTypeOfCompany];
                        UITabBarController *tabBarController = (UITabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
                        tabBarController.selectedIndex = 1;
                    }];
                } else if ([data[@"user_guide"] isEqualToString:@"company_create"]) { // 招聘者下面没有企业的话就会引导创建
                    CompanyFirstInfoViewController *IDSVC = [[CompanyFirstInfoViewController alloc] init];
                    IDSVC.hidesBottomBarWhenPushed = YES;
                    [self.navigationController pushViewController:IDSVC animated:NO];
                } else if ([data[@"user_guide"] isEqualToString:@"company_info"]) { // 填写企业信息
                    CompanySecondInfoViewController *IDSVC = [[CompanySecondInfoViewController alloc] init];
                    IDSVC.hidesBottomBarWhenPushed = YES;
                    [self.navigationController pushViewController:IDSVC animated:NO];
                } else if ([data[@"user_guide"] isEqualToString:@"company_audit"]) { // 企业未认证
                    SVPHUDShow;
                    [DataToolsControl getMyBaseInfoForCompany:^(BOOL requestFlag) {
                        SVPHUDDismiss;
                        [SharedApp showViewController:TabBarTypeOfCompany];
                        UITabBarController *tabBarController = (UITabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
                        tabBarController.selectedIndex = tabBarController.viewControllers.count-1;
                        RootNavigationController *navigationController = (RootNavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;
                        
                        CompanyInfoViewController *CIVC = [[CompanyInfoViewController alloc] init];
                        CIVC.hidesBottomBarWhenPushed = YES;
                        [navigationController pushViewController:CIVC animated:YES];
                    }];
                } else if (([data[@"user_guide"] isEqualToString:@"resume_create"])) { // 创建简历页
                    PersonalFirstInfoViewController *PMRVC = [[PersonalFirstInfoViewController alloc] init];
                    PMRVC.hidesBottomBarWhenPushed = YES;
                    [self.navigationController pushViewController:PMRVC animated:NO];
                } else if ([data[@"user_guide"] isEqualToString:@"resume_edu"]) { // 完善简历中教育经历
                    if (data[@"is_graduate"] && ([data[@"is_graduate"] integerValue] == 0)) {
                        [DataToolsControl sharedManager].idType = IDTypeOfWorker;
                    } else {
                        [DataToolsControl sharedManager].idType = IDTypeOfFreshGraduate;
                    }
                    
                    PersonalEducationInfoViewController *PMRVC = [[PersonalEducationInfoViewController alloc] init];
                    PMRVC.hidesBottomBarWhenPushed = YES;
                    [self.navigationController pushViewController:PMRVC animated:NO];
                } else if ([data[@"user_guide"] isEqualToString:@"resume_work"]) { // 完善简历中工作经历
                    PersonalWorkExperienceViewController *PMRVC = [[PersonalWorkExperienceViewController alloc] init];
                    PMRVC.hidesBottomBarWhenPushed = YES;
                    [self.navigationController pushViewController:PMRVC animated:NO];
                } else { 
                    [DataToolsControl getMyBaseInfoForCompany:^(BOOL requestFlag) {
                        [SharedApp showViewController:TabBarTypeOfCompany];
                    }];
                }
            }
        } else {
            SVPHUDError(responseObject[@"msg"]);
        }
    } failurBlock:^(NSError *error) {
        SVPHUDError(NetErrorAlertText);
    }];
}
- (void)pushIntentionVC {
    RegisterIntentionViewController *RIVC = [[RegisterIntentionViewController alloc] init];
    RIVC.delegate = self;
    if (RegisterIntentionModel) {
        RIVC.infoDataModel = [RegisterIntentionModel copy];
    }
    [self.navigationController pushViewController:RIVC animated:YES];
}
- (void)registerIntentionViewControllerBackData:(PMineResumeIntentionInfoModel *)infoModel {
    RegisterIntentionModel = [infoModel copy];
    self.workCell.textField.text = RegisterIntentionModel.jobs;
}
- (void)customBackButtonTap {
    [self.navigationController popViewControllerAnimated:YES];
}

@end
